/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GoogleDocsImportExport.java
 *
 * Created on 24-mei-2009, 13:28:51
 */

package Gui;

import Gui.login.ZipFileFilter;
import googleDocs.UploadToGD;
import importexport.Import;
import java.io.File;
import java.util.Vector;
import javax.swing.BorderFactory;

/**
 *
 * @author LESLEY
 */
public class GoogleDocsImportExport extends javax.swing.JFrame {
	
    public javax.swing.JPanel loginPanel;
    public int loginPanelHeight;
    public boolean loginImport;

    /** Creates new form GoogleDocsImportExport */
    public GoogleDocsImportExport(boolean isImport) {
        
    	loginImport = false;
    	filenamesGD = new Vector<String>();
    	
        initComponents();
        
        docsList.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        
        pack();
        setLocationRelativeTo(null);
        
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        
        loginPanelHeight = loginPanel.getHeight() + 15;
        importPanelHeight = importPanel.getHeight() + 5;
        exportPanelHeight = exportPanel.getHeight() + 5;
        if (isImport) {
            initWithImportPanel();
        } else {
            initWithExportPanel();
        }
        initFileChooser();
    }
    
    public final void setLoginData(String username, String password) {
    	this.username = username;
    	this.password = password;
    }

    private final void initFileChooser() {
        localFileChooser.setMultiSelectionEnabled(true);
        localFileChooser.setAcceptAllFileFilterUsed(false);
        localFileChooser.setDialogTitle("USIM Pro - Lokaal bestand zoeken...");
    }
    
    private final void initWithImportPanel() {
        localFileChooser.addChoosableFileFilter(new ZipFileFilter());
        
        exportPanel.setVisible(false);
        setSize(getWidth(), getHeight() - exportPanelHeight);
    }
    
    private final void initWithExportPanel() {
        localFileChooser.addChoosableFileFilter(new GDFileFilter());
        
        importPanel.setVisible(false);
        setSize(getWidth(), getHeight() - importPanelHeight);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        localFileChooser = new javax.swing.JFileChooser();
        loginPanel = new javax.swing.JPanel();
        searchButton = new javax.swing.JButton();
        loginPasswordLabel = new javax.swing.JLabel();
        loginUsernameLabel = new javax.swing.JLabel();
        loginUsernameTextField = new javax.swing.JTextField();
        loginPasswordField = new javax.swing.JPasswordField();
        importPanel = new javax.swing.JPanel();
        docsListScrollPane = new javax.swing.JScrollPane();
        docsList = new javax.swing.JList();
        importButton = new javax.swing.JButton();
        importCancelButton = new javax.swing.JButton();
        exportPanel = new javax.swing.JPanel();
        exportCancelButton = new javax.swing.JButton();
        exportButton = new javax.swing.JButton();
        exportLabel = new javax.swing.JLabel();
        exportTextField = new javax.swing.JTextField();
        browseButton = new javax.swing.JButton();

        localFileChooser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                localFileChooserActionPerformed(evt);
            }
        });

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("USIM Pro - Google Docs Import");
        setResizable(false);

        loginPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Login", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 0, 0))); // NOI18N

        searchButton.setText("Verbinding maken");
        searchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchButtonActionPerformed(evt);
            }
        });

        loginPasswordLabel.setText("Wachtwoord: ");

        loginUsernameLabel.setText("Gebruikersnaam: ");

        javax.swing.GroupLayout loginPanelLayout = new javax.swing.GroupLayout(loginPanel);
        loginPanel.setLayout(loginPanelLayout);
        loginPanelLayout.setHorizontalGroup(
            loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(loginPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(loginPanelLayout.createSequentialGroup()
                        .addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(loginPasswordLabel)
                            .addComponent(loginUsernameLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(loginUsernameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE)
                            .addComponent(loginPasswordField, javax.swing.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE)))
                    .addComponent(searchButton, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        loginPanelLayout.setVerticalGroup(
            loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(loginPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(loginUsernameLabel)
                    .addComponent(loginUsernameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(loginPasswordLabel)
                    .addComponent(loginPasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(searchButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        importPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Bestanden op Google Docs", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 0, 0))); // NOI18N

        docsList.setVisibleRowCount(10);
        docsListScrollPane.setViewportView(docsList);

        importButton.setText("Importeren");
        importButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                importButtonActionPerformed(evt);
            }
        });

        importCancelButton.setText("Annuleren");
        importCancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                importCancelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout importPanelLayout = new javax.swing.GroupLayout(importPanel);
        importPanel.setLayout(importPanelLayout);
        importPanelLayout.setHorizontalGroup(
            importPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(importPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(importPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(docsListScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 313, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, importPanelLayout.createSequentialGroup()
                        .addComponent(importButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(importCancelButton)))
                .addContainerGap())
        );
        importPanelLayout.setVerticalGroup(
            importPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(importPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(docsListScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(importPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(importButton)
                    .addComponent(importCancelButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        exportPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Exporteren naar Google Docs", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 0, 0))); // NOI18N

        exportCancelButton.setText("Annuleren");
        exportCancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exportCancelButtonActionPerformed(evt);
            }
        });

        exportButton.setText("Exporteren");
        exportButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exportButtonActionPerformed(evt);
            }
        });

        exportLabel.setText("Bestand: ");

        browseButton.setText("Bladeren...");
        browseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                browseButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout exportPanelLayout = new javax.swing.GroupLayout(exportPanel);
        exportPanel.setLayout(exportPanelLayout);
        exportPanelLayout.setHorizontalGroup(
            exportPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(exportPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(exportPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, exportPanelLayout.createSequentialGroup()
                        .addComponent(exportButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(exportCancelButton))
                    .addGroup(exportPanelLayout.createSequentialGroup()
                        .addComponent(exportLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(exportTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 263, Short.MAX_VALUE))
                    .addComponent(browseButton, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        exportPanelLayout.setVerticalGroup(
            exportPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, exportPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(exportPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(exportLabel)
                    .addComponent(exportTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(browseButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
                .addGroup(exportPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(exportCancelButton)
                    .addComponent(exportButton))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(loginPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(importPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(exportPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(loginPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(importPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(exportPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void importCancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_importCancelButtonActionPerformed
        dispose();
    }//GEN-LAST:event_importCancelButtonActionPerformed

    private void exportCancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exportCancelButtonActionPerformed
        dispose();
    }//GEN-LAST:event_exportCancelButtonActionPerformed

    private void importButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_importButtonActionPerformed
        Object[] v = docsList.getSelectedValues();
        for (int i = 0; i < v.length; i++) {
            if (v[i] instanceof String) {
                filenamesGD.add( (String)v[i] );
            }
        }
        for (int i = 0; i < filenamesGD.size(); i++) {
            try {
                UploadToGD.getGoogleDocsFile(username, password, filenamesGD.elementAt(i));
            } catch (Exception e) {
                // TODO filename niet bestaand op GD of fout met internetverbinding
                return;
            }
        }
        for (int i = 0; i < filenamesGD.size(); i++) {
            try {
                if (!Import.importZip("src/GoogleDocs/" + filenamesGD.elementAt(i))) {
                    return;
                }
            } catch (Exception e) {
                // TODO popup window -> fout in importeren
                return;
            }
        }
    }//GEN-LAST:event_importButtonActionPerformed

    private void exportButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exportButtonActionPerformed
        // TODO upload via GD
        for (int i = 0; i < localFiles.length; i++) {
            try {
                UploadToGD.uploadFile(username, password, localFiles[i].getAbsolutePath());
            } catch (Exception e) {
                // TODO popup window -> fout met internetverbinding
                return;
            }
        }
    }//GEN-LAST:event_exportButtonActionPerformed

    public void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButtonActionPerformed
        if (!loginImport) {
	    	username = loginUsernameTextField.getText();
	        password = loginPasswordField.getText();
        }
        try {
            filenamesGD = UploadToGD.getGoogleDocsContent(username, password);
        } catch (Exception e) {
            // TODO popup window -> login gegevens incorrect of geen internetverbinding
        	e.printStackTrace();
            filenamesGD = new Vector<String>();
        }
        docsList.setListData(filenamesGD);
    }//GEN-LAST:event_searchButtonActionPerformed

    private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseButtonActionPerformed
        localFileChooser.showOpenDialog(this);
}//GEN-LAST:event_browseButtonActionPerformed

    private void localFileChooserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_localFileChooserActionPerformed
        localFiles = localFileChooser.getSelectedFiles();
        String filenames = "";
        for (int i = 0; i < localFiles.length - 1; i++) {
            filenames += "\"" + localFiles[i].getName() + "\"; ";
        }
        if (localFiles.length > 0) {
            filenames += "\"" + localFiles[localFiles.length - 1].getName()+ "\"";
        }
        exportTextField.setText(filenames);
    }//GEN-LAST:event_localFileChooserActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                // TODO change
                new GoogleDocsImportExport(true).setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton browseButton;
    private javax.swing.JList docsList;
    private javax.swing.JScrollPane docsListScrollPane;
    private javax.swing.JButton exportButton;
    private javax.swing.JButton exportCancelButton;
    private javax.swing.JLabel exportLabel;
    private javax.swing.JPanel exportPanel;
    private javax.swing.JTextField exportTextField;
    private javax.swing.JButton importButton;
    private javax.swing.JButton importCancelButton;
    private javax.swing.JPanel importPanel;
    private javax.swing.JFileChooser localFileChooser;
    private javax.swing.JPasswordField loginPasswordField;
    private javax.swing.JLabel loginPasswordLabel;
    private javax.swing.JLabel loginUsernameLabel;
    private javax.swing.JTextField loginUsernameTextField;
    private javax.swing.JButton searchButton;
    // End of variables declaration//GEN-END:variables
    private String username;
    private String password;
    private Vector<String> filenamesGD;
    private File[] localFiles;
    private int importPanelHeight;
    private int exportPanelHeight;
}
